//
//  HLInviteViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLInviteViewController.h"
#import "HLInviteRequest.h"

@interface HLInviteViewController ()

@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) UIImageView *infoView;
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) QMUIButton *idView;

@property (nonatomic, strong) UIImageView *inviteView;
@property (nonatomic, strong) UILabel *inviteTitleLabel;
@property (nonatomic, strong) QMUITextField *inviteTextField;
@property (nonatomic, strong) UILabel *inviteTipLabel;
@property (nonatomic, strong) QMUIButton *startButton;

@end

@implementation HLInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.request = [[HLInviteRequest alloc] initWithInfo];
    self.request.delegate = self;
    [self requestInit];
}

- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}


- (void)setupNavigationItems
{
    [super setupNavigationItems];
    self.title = @"我的邀请人";
}


- (void)initSubviews
{
    [super initSubviews];
    self.backgroundView = [[UIImageView alloc] initWithImage:UIImageMake(@"invite_background")];
    [self.view addSubview:_backgroundView];
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    CGSize size = CGSizeMake((SCREEN_WIDTH-24), 100);
    self.infoView = [[UIImageView alloc] init];
    _infoView.userInteractionEnabled = YES;
    _infoView.image =  [UIImage qmui_imageWithGradientColors:@[[UIColor qmui_colorWithHexString:@"#EAFAFD"], [UIColor qmui_colorWithHexString:@"#EBE8FD"]] type:QMUIImageGradientTypeHorizontal locations:@[@(0), @(1)] size:size cornerRadiusArray:@[@12, @12, @12, @12]];
    [self.view addSubview:_infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(NavigationContentTopConstant + 30);
    }];
    
    self.avatarView = [[UIImageView alloc] initWithImage:UIImageMake(@"invite_avatar")];
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 34;
    _avatarView.layer.borderColor = UIColorWhite.CGColor;
    _avatarView.layer.borderWidth = 2;
    [_infoView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(16);
        make.size.mas_equalTo(68);
    }];
    
    self.nickNameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(16) textColor:HLTextColor1];
    _nickNameLabel.text = @"暂无邀请人";
    [_infoView addSubview:_nickNameLabel];
    [_nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatarView).offset(8);
        make.left.equalTo(_avatarView.mas_right).offset(12);
        make.right.mas_equalTo(-16);
    }];
    
    self.contentLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:HLTextColor2];
    _contentLabel.numberOfLines = 0;
    _contentLabel.qmui_lineHeight = 18;
    _contentLabel.text = @"目前您还没有邀请人，您可以添加新的邀请人加入他的团队";
    [_infoView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_nickNameLabel);
        make.top.equalTo(_nickNameLabel.mas_bottom).offset(4);
    }];
    
    self.idView = [HLUIHelper iconIDViewWithId:@"12345"];
    _idView.hidden = YES;
    [_infoView addSubview:_idView];
    [_idView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickNameLabel);
        make.bottom.equalTo(_avatarView).offset(-12);
    }];
    
    CGSize inviteSize = CGSizeMake((SCREEN_WIDTH-24), 238);
    self.inviteView = [[UIImageView alloc] init];
    _inviteView.userInteractionEnabled = YES;
    _inviteView.image =  [UIImage qmui_imageWithGradientColors:@[[UIColor qmui_colorWithHexString:@"#EAFAFD"], [UIColor qmui_colorWithHexString:@"#EBE8FD"]] type:QMUIImageGradientTypeHorizontal locations:@[@(0), @(1)] size:size cornerRadiusArray:@[@12, @12, @12, @12]];
    [self.view addSubview:_inviteView];
    [_inviteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(inviteSize);
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_infoView.mas_bottom).offset(20);
    }];
    
    self.inviteTitleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(16) textColor:HLTextColor1];
    _inviteTitleLabel.text = @"填写邀请人邀请码";
    [_inviteView addSubview:_inviteTitleLabel];
    [_inviteTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(24);
    }];
    
    self.inviteTextField = [[QMUITextField alloc] init];
    [_inviteTextField setBackgroundColor:HLBackgroundColor];
    [_inviteTextField setPlaceholder:@"请输入邀请人邀请码"];
    [_inviteTextField setPlaceholderColor:HLTextColor3];
    _inviteTextField.layer.masksToBounds = YES;
    _inviteTextField.layer.cornerRadius = 12;
    _inviteTextField.maximumTextLength = 6;
    _inviteTextField.backgroundColor = UIColorWhite;
    _inviteTextField.textInsets = UIEdgeInsetsMake(10, 16, 10, 16);
    [_inviteView addSubview:_inviteTextField];
    [_inviteTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(50);
        make.top.equalTo(_inviteTitleLabel.mas_bottom).offset(20);
    }];
    
    self.inviteTipLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(12) textColor:HLTextColor3];
    _inviteTipLabel.text = @"您已填写邀请人，暂时不能修改";
    _inviteTipLabel.hidden = YES;
    [_inviteView addSubview:_inviteTipLabel];
    [_inviteTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_inviteTextField.mas_bottom).offset(8);
    }];
    
    self.startButton = [[QMUIButton alloc] init];
    [_startButton setTitle:@"确认" forState:UIControlStateNormal];
    [_startButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_startButton.titleLabel setFont:UIFontMediumMake(15)];
    [_startButton addTarget:self action:@selector(inviteAction) forControlEvents:UIControlEventTouchUpInside];
    _startButton.cornerRadius = 26;
    [_startButton setBackgroundColor:HLMainColor];
    [_inviteView addSubview:_startButton];
    [_startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(166, 52));
        make.bottom.mas_equalTo(-30);
    }];
    
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLInviteRequest *inviteRequest = (HLInviteRequest *)request;

    if ([inviteRequest isSuccess]){
        [self hideEmptyView];
        HLInviteModel *invide = inviteRequest.invite;
        if (invide){
            [self.avatarView sd_setImageWithURL:[NSURL URLWithString:invide.agentAvatar]];
            [self.nickNameLabel setText:invide.agentNickName];
            [self.contentLabel setHidden:YES];
            [self.idView setHidden:NO];
            [self.idView setTitle:invide.agentYueId forState:UIControlStateNormal];
            [self.inviteTipLabel setHidden:NO];
            [self.inviteTextField setEnabled:NO];
            [self.inviteTextField setText:invide.agentInviteNo];
            [self.startButton setEnabled:NO];
        }
    } else {
        [self.emptyView setLoadingViewHidden:YES];
        [self.emptyView setImage:UIImageMake(@"empty_icon_7")];
        [self.emptyView setTextLabelText:inviteRequest.message];
    }
    
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

- (void)inviteAction
{
    // 33373V;
    NSString *inviteCode =  _inviteTextField.text;
    if (inviteCode.length == 0){
        [QMUITips showWithText:@"请填写邀请码"];
        return;
    }
    
    if (inviteCode.length != 6){
        [QMUITips showWithText:@"请填写正确的邀请码"];
        return;
    }
    

    HLInviteRequest *userOptionRequest = [[HLInviteRequest alloc] initWithInviteNo:inviteCode];
    [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLInviteRequest * _Nonnull request) {
        if (request.isSuccess){
            [QMUITips showWithText:@"绑定邀请人成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(__kindof HLInviteRequest * _Nonnull request) {
        [QMUITips showWithText:request.message];
    }];
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorClear];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorWhite;
}

- (nullable UIColor *)qmui_titleViewTintColor
{
    return UIColorWhite;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
